package com.xqh.service.job;

import com.xqh.base.pool.ThreadPoolUtils;
import com.xqh.entity.ques.Vocabulary;
import com.xqh.service.ques.VocabularyService;
import com.xqh.spider.SpiderWordThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@Service
public class VocabularyJobService {

    private static final Logger logger = LoggerFactory.getLogger(VocabularyJobService.class);

    @Autowired
    private VocabularyService vocabularyService;


    /**
     * 下载音频 任务
     */
    public void downloadAudioTask(){
        logger.info("更新下载词汇音频任务 start ===============>");
        Map<String,Object> map = new HashMap<>();
        map.put("needAudio","yes");//查找未下载音频的 词汇
        List<Vocabulary> vlist = vocabularyService.findByBiz(map);
        logger.info("未下载音频数据有:"+vlist.size()+" 条");
        if(!CollectionUtils.isEmpty(vlist)){
            for(Vocabulary vo:vlist){
                DownloadAudioThread thread = new DownloadAudioThread(vocabularyService, vo);
                ThreadPoolUtils.execute(thread);
            }
        }
        logger.info("更新下载词汇音频任务 end ===============>");
    }

    @Transactional
    public void reUpdateTask() throws ExecutionException, InterruptedException {
        logger.info("更新信息不全词汇数据 start ===============>");
        Map<String,Object> map = new HashMap<>();
        map.put("defectData","yes");
        map.put("lessNum", 5);
        map.put("limitNum", 50);//一次执行50条
        List<Vocabulary> defectList = vocabularyService.findByBiz(map);
        if(!CollectionUtils.isEmpty(defectList)){
            vocabularyService.batchUpdateVocab(defectList);
        }
        logger.info("信息不全数据有:"+defectList.size()+" 条");
        List<FutureTask<Vocabulary>> flist = new ArrayList<>();

        for(Vocabulary vo:defectList){
            SpiderWordThread spider = new SpiderWordThread(vo.getText());
            FutureTask<Vocabulary> future = new FutureTask<>(spider);
            ThreadPoolUtils.submit(future);
            flist.add(future);
        }

        for(FutureTask<Vocabulary> future:flist){
            Vocabulary vocab = future.get();
            ReUpdateThread reThread = new ReUpdateThread(vocabularyService, vocab);
            ThreadPoolUtils.execute(reThread);
        }
        logger.info("更新信息不全词汇数据 end ===============>");
    }

    public static class ReUpdateThread implements Runnable{

        private VocabularyService vocabularyService;

        private Vocabulary vocabulary;

        public ReUpdateThread(VocabularyService vocabularyService, Vocabulary vocabulary){
            this.vocabulary = vocabulary;
            this.vocabularyService = vocabularyService;
        }
        @Override
        public void run() {
           vocabularyService.reUpdateData(vocabulary);

        }
    }

    public static class DownloadAudioThread implements Runnable{

        private VocabularyService vocabularyService;

        private Vocabulary vocabulary;

        public DownloadAudioThread(VocabularyService vocabularyService, Vocabulary vocabulary){
            this.vocabulary = vocabulary;
            this.vocabularyService = vocabularyService;
        }

        @Override
        public void run() {
           vocabularyService.downloadAudio(vocabulary);
        }
    }
}
